<?php

namespace App\Http\Controllers\Api;

use App\Service\ArticleService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    /**
     * @api {post} api/article/create  添加文章
     * @apiVersion 1.0.0
     * @apiName createArticle
     * @apiGroup Article
     *
     * @apiParam   {String} user_id 用户id.
     * @apiParam   {String} content 内容.
     * @apiParam   {String} title 标题.
     * @apiParam   {String} [class_id] 分类id.
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function createArticle(Request $request)
    {
        $input = $request->all();

        $rules = array(
            "user_id" => "required",
            "content" => "required",
            "title" => "required",
        );
        $messages = array(
            "user_id.required" => "用户为空",
            "content.required" => "内容为空",
            "title.required" => "标题为空");
        $result = $this->validate($input, $rules, $messages);
        if ($result["code"] != 0) {
            return $this->fail(self::VALIDATE_ERROR, $result["errors"]);
        }

        $res = ArticleService::createArticle($input);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '添加失败');
        }
    }

    /**
     * @api {post} api/article/{id}/delete  删除Article
     * @apiVersion 1.0.0
     * @apiName deleteArticle
     * @apiGroup Article
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function deleteArticle(Request $request, $id)
    {
        $res = ArticleService::deleteArticle($id);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '删除失败');
        }
    }

    /**
     * @api {post} api/article/{id}/update  修改Article
     * @apiVersion 1.0.0
     * @apiName updateArticle
     * @apiGroup Article
     *
     * @apiParam   {String} [status] 发布文章.
     * @apiParam   {String} [title] 文章名称.
     * @apiParam   {String} [content] 文章内容.
     * @apiParam   {String} [class_id] 分类名称.
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function updateArticle(Request $request, $id)
    {
        $input = $request->all();

        $updateInfo = array();
        if (!empty($input['class_id'])) {
            $updateInfo['class_id'] = intval($input['class_id']);
        }
        if (!empty($input['status'])) {
            $updateInfo['status'] = intval($input['status']);
        }
        if (!empty($input['title'])) {
            $updateInfo['title'] = $input['title'];
        }
        if (!empty($input['content'])) {
            $updateInfo['content'] = $input['content'];
        }

        $res = ArticleService::updateArticle($id, $updateInfo);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '修改失败');
        }
    }

    /**
     * @api {get} api/article/list  获取Article列表
     * @apiVersion 1.0.0
     * @apiName getArticleList
     * @apiGroup Article
     * @apiParam   {String} [status] 状态 1：未发布，2，已发布，-1 全部.
     * @apiParam   {String} [class_name] 分类名称.
     * @apiParam   {String} [user_id] 查看某个人发布的文章.
     * @apiParam   {int} [page_limit=100] 每页显示的个数 默认为100.
     * @apiParam   {int} [page=1] 默认显示第一页
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function getArticleList(Request $request)
    {
        $input = $request->all();

        $page_limit = empty($request->input('page_limit')) ? 100 : $request->input('page_limit');

        $condition = array();
        if (isset($input['status'])) {
            if (intval($input['status']) != -1) {
                $condition['status'] = $input['status'];
            }
        }
        if (isset($input['class_id'])) {
            if (intval($input['class_id']) != -1) {
                $condition['class_id'] = $input['class_id'];
            }
        }
        if (isset($input['user_id'])) {
            $condition['user_id'] = $input['user_id'];
        }

        $res = ArticleService::getArticleList($condition, $page_limit);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '获取失败');
        }
    }

    /**
     * @api {post} api/article/class/create  添加文章分类
     * @apiVersion 1.0.0
     * @apiName createArticleClass
     * @apiGroup Article
     *
     * @apiParam   {String} user_id 用户id.
     * @apiParam   {String} name 内容.
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function createArticleClass(Request $request)
    {
        $input = $request->all();

        $rules = array(
            "user_id" => "required",
            "name" => "required",
        );
        $messages = array(
            "user_id.required" => "用户为空",
            "name.required" => "内容为空",);
        $result = $this->validate($input, $rules, $messages);
        if ($result["code"] != 0) {
            return $this->fail(self::VALIDATE_ERROR, $result["errors"]);
        }

        $res = ArticleService::createArticleClass($input['user_id'], $input['name']);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '添加失败');
        }
    }

    /**
     * @api {post} api/article/class/{id}/delete  删除Article分类
     * @apiVersion 1.0.0
     * @apiName deleteArticleClass
     * @apiGroup Article
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function deleteArticleClass(Request $request, $id)
    {
        $res = ArticleService::deleteArticleClass($id);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '删除失败');
        }
    }

    /**
     * @api {post} api/article/class/{id}/update  修改Article分类
     * @apiVersion 1.0.0
     * @apiName updateArticleClass
     * @apiGroup Article
     *
     * @apiParam   {String} [name] 发布文章.
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function updateArticleClass(Request $request, $id)
    {
        $input = $request->all();

        $updateInfo = array();
        if (!empty($input['name'])) {
            $updateInfo['name'] = $input['name'];
        }

        $res = ArticleService::updateArticleClass($id, $updateInfo);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '修改失败');
        }
    }

    /**
     * @api {get} api/article/list  获取Article列表
     * @apiVersion 1.0.0
     * @apiName getArticleClassList
     * @apiGroup Article
     * @apiParam   {String} user_id 查看某个人发布的文章.
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function getArticleClassList(Request $request)
    {
        $input = $request->all();


        $condition = array();

        if (isset($input['user_id'])) {
            $condition['user_id'] = $input['user_id'];
        } else {
            return $this->fail(self::VALIDATE_ERROR, "用户id为空");
        }

        $res = ArticleService::getArticleClassListByUser($input['user_id']);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '获取失败');
        }
    }
}
